package com.kunya.product.service.impl.product;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.product.ProductAttributeMapper;
import com.kunya.product.po.ProductAttribute;
import com.kunya.product.service.product.IProductAtrributeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zp
 * @Description: 商品属性service
 * @date 2018/6/13 15:21
 */
@Service("iProductAtrributeService")
public class ProductAtrributeServcieImpl implements IProductAtrributeService {

    @Autowired
    private ProductAttributeMapper productAttributeMapper;


    /**
     * @param productId
     * @author zp
     * @Description: 根据商品id获取商品属性实体
     * @date 2018/6/13 15:25
     */
    public ServerResponse<ProductAttribute> getProductAttributeByProductId(Long productId, String tablePrefix) {
        if(productId==null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        ProductAttribute productAttribute=productAttributeMapper.selectByProductId(productId,tablePrefix);
        if (productAttribute==null){
            return ServerResponse.createByErrorMessage("商品属性不存在");
        }
        return ServerResponse.createBySuccess(productAttribute);
    }


    /**
     * @author zp
     * @Description: 插入商品属性表
     * @param
     * @date 2018/7/2 13:12
     */
    public ServerResponse saveProductAttribute(ProductAttribute productAttribute,String tablePrefix){
        if(productAttribute==null|| StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        int reusltCount=productAttributeMapper.insertSelective(productAttribute,tablePrefix);
        if(reusltCount<=0){
            return ServerResponse.createByErrorMessage("商品属性插入失败");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteProductAttributeByProductId(Long productId, String tablePrefix) {
        productAttributeMapper.deleteProductAttributeByProductId(productId,tablePrefix);
        return ServerResponse.createBySuccess("删除商品属性成功");
    }
}
